using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_QuestTargetSceneMark : UIBaseNode
    {
        private Sprite _defaultIcon;

        private Transform _bindPoint;

        private TViewEntity _entity;

        private const float MarginRatio = 0.2f;

        private static float _marginX = Screen.width * MarginRatio;

        private static float _marginY = Screen.height * MarginRatio;
        #region binding

        public ExText txtDistance { protected set; get; }
        public ExImage icon { protected set; get; }

        public virtual void InitBinding(ObjectBinding __binding)
        {
            __binding.TryGetVariableValue<ExText>("txtDistance", out var __tbv0);
            txtDistance = __tbv0;
            __binding.TryGetVariableValue<ExImage>("icon", out var __tbv1);
            icon = __tbv1;
        }

        #endregion

        protected override void BeforeInit()
        {
            InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            _defaultIcon = icon.sprite;
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {
            _bindPoint = null;
            _entity = null;
        }

        public void SetData(TViewEntity entity)
        {
            _entity = entity;
            UpdateManualTrackEntityIcon(entity);
        }

        private Vector3 GetEntityHeadPosition()
        {
            //如果实体的transform有效，则获取挂点
            if (!_entity.transform.isValueNull)
            {
                //不重复获取
                if (_bindPoint == null)
                {
                    _bindPoint = BindPointUtil.GetBindPoint(EntityBindPointCommonType.Top, _entity.transform);
                }
            }
            //如果实体的transform无效，清空挂点
            else
            {
                _bindPoint = null;
            }
            //如果有挂点，返回挂点位置，否则返回实体数据里的位置 + 默认高度
            if (_bindPoint != null)
            {
                return _bindPoint.position;
            }
            return _entity.unityPosOfData + new Vector3(0, 1, 0);
        }

        public void Update()
        {
            //实时获取玩家化身，因为可能切换队长
            var avatarTrans = GameplayAreaManager.Instance.curSceneArea.GetPlayerAvatarEntity().transform;
            var avatarPos = avatarTrans == null ? Vector3.zero : avatarTrans.position;
            var bindPointPos = GetEntityHeadPosition();
            transform.localPosition = ManagerCenter.UI.World2UIPosition(bindPointPos, _marginX, _marginY);
            var distance = Vector3.Distance(avatarPos, bindPointPos);
            txtDistance.text = ((int)Mathf.Round(distance)).ToString();
        }

        public void UpdateManualTrackEntityIcon(TViewEntity entity)
        {
            var trackingEntity = GameplayAreaManager.Instance.curSceneArea.GetTrackingEntity();
            if (trackingEntity == entity)
            {
                SetImageSprite(icon, TableCenter.mapUiEntity.Get(entity.mapSymbolCid).Icon_fullPath);
            }
            else
            {
                icon.sprite = _defaultIcon;
            }
        }

        protected override void OnDispose()
        {
        }
    }
}